package com.orhon.smartcampus.modules.user.fetcher;


import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.gqlModel.GQLAddressBook;
import com.orhon.smartcampus.modules.user.mm.AddressBookMapper;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class AddressBookFetcher implements DataFetcher {


    @Autowired
    TokenService tokenService;


    @Autowired
    TInformationDao tInformationDao;


    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        String token = tokenService.getSystemToken();
        if (token == null){
            return R.NoLogin();
        }
        List<GQLAddressBook> ret = new ArrayList();
        Long schoolId = JwtUtils.getSchoolId(token);
        if (schoolId > 0){
            List<TInformation> teacherList = tInformationDao.find("from TInformation where school_id = " + schoolId);
            ret = AddressBookMapper.MAPPER.getAddressFromTeachers(teacherList);
        }
        return Result.ok().put("data" , ret);
    }


}
